package com.yunfeng.supercall.manager;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.yunfeng.supercall.bean.AdsBean;
import com.yunfeng.supercall.bean.SuperFriendsBean;

public class ManagerFactory {

	public static final Map<Class<?>, ManagerBaseIface> MANAGERS = new HashMap<Class<?>, ManagerBaseIface>();

	public static ManagerBaseIface createManager(Class<?> clz) {
		ManagerBaseIface mIface = MANAGERS.get(clz);
		if (mIface == null) {
			// if (clz.equals(Ads.class.getClass())) {
			// mIface = new ManagerBaseIface();
			// }
			if (clz.equals(SuperFriendsBean.class)) {
				mIface = new SuperFriendsManager();
			} else if (clz.equals(AdsBean.class)) {
				mIface = new AdsManager();
			} else {
				mIface = new ManagerBaseIface();
			}

			MANAGERS.put(clz, mIface);
		}
		return mIface;
	}

	public static final void destory(Class<?> clz) {
		createManager(clz).destory();
	}

	public static final void clear(Class<?> clz) {
		createManager(clz).clear();
	}

	public static final void clearAll() {
		try {
			Iterator iterator = MANAGERS.keySet().iterator();
			while (iterator.hasNext()) {
				Map.Entry entry = (Map.Entry) iterator.next();
				Object key = entry.getKey();
				ManagerBaseIface val = (ManagerBaseIface) entry.getValue();
				val.clear();
			}
			MANAGERS.clear();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static final void destoryAll() {
		Iterator iterator = MANAGERS.keySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry entry = (Map.Entry) iterator.next();
			Object key = entry.getKey();
			ManagerBaseIface val = (ManagerBaseIface) entry.getValue();
			val.destory();
		}
		MANAGERS.clear();
	}
}
